package sysytmRepo

import (
	"fmt"
	"gorm.io/gorm"
	"ruoyi/domain/entity/common"
	"ruoyi/domain/entity/response"
	"ruoyi/domain/entity/system"
	"ruoyi/domain/repository"
	"ruoyi/utils"
)

type SysDictDataRepo struct {
	db *gorm.DB
}

func SysDictDataRepository(db *gorm.DB) *SysDictDataRepo {
	return &SysDictDataRepo{db}
}

var _ repository.SysDictDataRepository = &SysDictDataRepo{}

func (s *SysDictDataRepo) SelectDictDataByType(dictType string) []system.SysDictData {
	var dictData []system.SysDictData
	s.db.Raw("select * from sys_dict_data where status = '0' and dict_type = ? order by dict_sort", dictType).Scan(&dictData)
	return dictData
}

// 分页查询
func (s *SysDictDataRepo) SelectDictDataList(params common.SearchTableDataParam, isPage bool) common.TableDataInfo {
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	sysDictData := params.Other.(system.SysDictData)
	offset := (pageNum - 1) * pageSize
	var total int64
	var rows []system.SysDictData

	var db = s.db.Model(&rows)

	var dictLabel = sysDictData.DictLabel
	if dictLabel != "" {
		db.Where("dict_label = ?", dictLabel)
	}
	var dictType = sysDictData.DictType
	if dictType != "" {
		db.Where("dict_type like concat('%', ?, '%')", dictType)
	}
	var status = sysDictData.Status
	if status != "" {
		db.Where("status = ?", status)
	}

	db.Order("dict_sort asc")

	if err := db.Count(&total).Error; err != nil {
		return common.Fail()
	}
	if isPage {
		if err := db.Limit(pageSize).Offset(offset).Find(&rows).Error; err != nil {
			return common.Fail()
		}
	} else {
		if err := db.Find(&rows).Error; err != nil {
			return common.Fail()
		}
	}

	if rows == nil {
		return common.Fail()
	} else {
		return common.Success(rows, total)
	}
}

func (s *SysDictDataRepo) FindDictCodeById(dictCode string) system.SysDictData {
	var dictData system.SysDictData
	err := s.db.Where("dict_code = ?", dictCode).First(&dictData).Error
	if err != nil {
		panic(response.ReturnFailMsg(err.Error()))
	}
	return dictData
}

func (s *SysDictDataRepo) SaveDictData(dictDataParam system.SysDictData) response.Response {
	isDefault := dictDataParam.IsDefault
	if isDefault == "" {
		dictDataParam.IsDefault = "N"
	}
	err := s.db.Model(&system.SysDictData{}).Create(&dictDataParam).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (s *SysDictDataRepo) EditDictData(dictDataParam system.SysDictData) response.Response {
	err := s.db.Updates(&dictDataParam).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (s *SysDictDataRepo) DeleteDictData(dictCodes string) response.Response {
	var ids = utils.Split(dictCodes)
	s.db.Begin()
	for i := 0; i < len(ids); i++ {
		id := ids[i]
		err := s.db.Where("dict_code = ?", id).Delete(&system.SysDictData{}).Error
		if err != nil {
			fmt.Println(s.db.Rollback().Error)
			return response.ReturnFailMsg(err.Error())
		}
	}
	fmt.Println(s.db.Commit().Error)
	return response.ReturnSuccess("操作成功")
	// 或
	// err := utils.MysqlDb.Where("dict_code in (?)", ids).Delete(&SysDictData{}).Error
	// if err != nil {
	// 	fmt.Println(utils.MysqlDb.Rollback().Error)
	// 	return R.ReturnFailMsg(err.Error())
	// }
	// return R.ReturnSuccess("操作成功")
}
